package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import domin.Blacklist;
import lombok.extern.slf4j.Slf4j;
import service.BlacklistsService;
import util.Condition;
import util.JSONUtil;
import util.Pagination;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;


@WebServlet("/blacklist.ctl")
@Slf4j
public class BlacklistController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");


        String id_str = request.getParameter("id");

        String keyword = request.getParameter("keyword");
        try {
            if(id_str!=null)
            {
                int id = Integer.parseInt(id_str);
                responseBlacklist(id, response);
                log.trace("查找id" + id);
            }
            else if(keyword !=null){
                this.responseBlacklist(keyword,response);
            }
            else {
                this.responseBlacklists(response, null, null);
            }
        } catch (SQLException e){

            log.error("数据库异常"+ e.getMessage());
        } catch(Exception e){

            log.error("message","网络异常" + e.getMessage());
        }
    }

    private void responseBlacklist(int id, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        Blacklist blacklist = BlacklistsService.getInstance().find(id);
        String bl_json = JSON.toJSONString(blacklist);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",bl_json);
        response.getWriter().println(jsonObject);
    }


    private void responseBlacklists(HttpServletResponse response, Collection<Condition> condition, Pagination pagination)
            throws ServletException, IOException, SQLException {

        Collection<Blacklist> blacklists = BlacklistsService.getInstance().findAll(condition,pagination);

        String bls_json = JSON.toJSONString(blacklists, SerializerFeature.DisableCircularReferenceDetect);
        response.getWriter().println(bls_json);
    }
    private void responseBlacklist(String keyword,HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        Collection<Blacklist> blacklists = BlacklistsService.getInstance().getAllWithCondition(keyword);

        String blacklists_json = JSON.toJSONString(blacklists, SerializerFeature.DisableCircularReferenceDetect);

        response.getWriter().println(blacklists_json);
    }


    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id_str = request.getParameter("id");
        int id = Integer.parseInt(id_str);
        response.setContentType("application/json;charset=UTF-8");
        JSONObject message = new JSONObject();
        try {
            BlacklistsService.getInstance().delete(id);
            message.put("message", "删除成功");
        }catch (SQLException e){
            message.put("message", "数据库操作异常");
        }catch(Exception e){
            message.put("message", "网络异常");
        }
        response.getWriter().println(message);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {

        request.setCharacterEncoding("UTF-8");

        String blacklist_json = JSONUtil.getJSON(request);

        System.out.println(blacklist_json);

        Blacklist blacklist = JSON.parseObject(blacklist_json, Blacklist.class);

        System.out.println(blacklist);

        response.setContentType("application/json;charset=UTF-8");

        JSONObject message = new JSONObject();

        try {
            BlacklistsService.getInstance().add(blacklist);
            message.put("message", "增加成功");
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");

            log.error(e.getMessage(),e);
        } catch (Exception e) {
            message.put("message", "网络异常");

            log.error(e.getMessage(),e);
        }

        response.getWriter().println(message);
    }

}
